﻿//////////////////////////////
//User interface object constructor class.
//Basic building blocks like boxes & menus etc.
//////////////////////////////
package org.bbdesign.airbuzz {
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	import flash.filters.*;
	import flash.geom.*;
	//////////////////////////////
	import fl.controls.*;
	import fl.data.*;
	import fl.motion.Color;
	import fl.managers.StyleManager;
	//////////////////////////////
	public class Interface extends Sprite {
		//////////////////////////////
		//Internal class variables.
		//////////////////////////////
		//Instance.
		private var _matte:Sprite = new Sprite();
		private var _buttn:Button = new Button();
		private var _checkbox:CheckBox = new CheckBox();
		private var _pb:ProgressBar;
		private var _list:List;
		private var _label:String;
		private var _btnname:String;
		private var _drawtype:String;
		private var _txtfont:Font;
		private var _textfield:TextField;
		private var _txtfldborder:Boolean;
		private var _txtfldbordercol:uint;
		private var _txtfldtype:String;
		private var _txtformat:TextFormat = new TextFormat();
		private var _xmldat:XML;
		private var _menutype:String;
		private var _menuholder:Object;
		private var _picker:ColorPicker = new ColorPicker();
		private var _slider:Slider = new Slider();
		private var _glowfilter:GlowFilter;
		private var _dropShadow:DropShadowFilter;
		private var _rdbtn:RadioButton;
		private var _value:Object;
		private var _vscrollbar:UIScrollBar;
		private var _object:*;
		//Custom class instances.
		private var _utility:Utility = new Utility();
		//////////////////////////////
		//Constructor.
		//////////////////////////////
		public function Interface () {
			//////////////////////////////
			//Initialize general filters.
			//////////////////////////////
			_glowfilter = _utility.initGlowFilter();
			_dropShadow = _utility.initDropShadowFilter();
			//////////////////////////////
		}
		//////////////////////////////
		//Build background matte/simple shapes.
		//////////////////////////////
		public function drawMatte(thick:Number, linecol:uint, col:uint, xpos:Number, ypos:Number, wd:Number, ht:Number, drawtype:String = 'rect', radwd:Number = undefined, radht:Number = undefined, alph:Number = 1.0, dpshadow:Boolean = false, nm:String = null):Sprite {
			_matte = new Sprite();
			_matte.graphics.lineStyle(thick, linecol);
			_matte.graphics.beginFill(col);
			switch (drawtype) {
				case 'roundrect' :
					_matte.graphics.drawRoundRect(0, 0, wd, ht, radwd, radht);
				break;
				case 'rect' :
					_matte.graphics.drawRect(0, 0, wd, ht);
				break;
			}
			_matte.graphics.endFill ();
			_matte.alpha =  alph;
			_matte.x = xpos;
			_matte.y = ypos;
			if(nm != null) {
				_matte.name = nm;
			}
			if(dpshadow) {
				_matte.filters = [_dropShadow];
			}
			_matte.cacheAsBitmap;
			return _matte;
		}
		//////////////////////////////
		//Build standard buttons.
		//////////////////////////////
		public function drawButton(font:Class, col:uint, txtfontsize:Number, btnlabel:String, xpos:Number, ypos:Number, wd:Number, btnemphasis:Boolean, sharp:Number = 0, thick:Number = 0):Button {
			_txtfont = new font;
			_txtformat.font = _txtfont.fontName;
			_txtformat.size = txtfontsize;
			_txtformat.color = col;

			_buttn.useHandCursor = true;
			_buttn.label = btnlabel;
			_buttn.x = xpos;
			_buttn.y = ypos;
			_buttn.width = wd;
			_buttn.alpha = .9;
			_buttn.emphasized = btnemphasis;
			_buttn.textField.antiAliasType = AntiAliasType.ADVANCED;
			_buttn.textField.sharpness = sharp;
			_buttn.textField.thickness = thick;
			_buttn.setStyle('textFormat', _txtformat);
			_buttn.setStyle('embedFonts', true);
			_buttn.cacheAsBitmap;

			_buttn.addEventListener (MouseEvent.MOUSE_OVER, btnOver);
			_buttn.addEventListener (MouseEvent.MOUSE_OUT, btnOut);

			function btnOver():void {
				_buttn.alpha = 1.0;
				_buttn.filters = [_dropShadow];
			}
			function btnOut():void {
				_buttn.alpha = .9;
				_buttn.filters = [];
			}
			return _buttn;
		}
		//////////////////////////////
		//Build progress bar.
		//////////////////////////////
		public function buildProgressBar(source:*, labelstring:String, xpos:int, ypos:int, font:Class, col:uint, txtfontsize:Number):ProgressBar {
			_txtfont = new font;
			_txtformat.font = _txtfont.fontName;
			_txtformat.size = txtfontsize;
			_txtformat.color = col;

			_pb = new ProgressBar();
			var pbLabel:Label = new Label();
			pbLabel.text = labelstring;
			pbLabel.autoSize = TextFieldAutoSize.LEFT;
			pbLabel.move(_pb.x, _pb.y + _pb.height);
			pbLabel.setStyle('textFormat', _txtformat);
			pbLabel.setStyle('embedFonts', true);
			_pb.addChild(pbLabel);

			_pb.source = source;
			_pb.x = xpos;
			_pb.y = ypos;
			_pb.cacheAsBitmap;
			return _pb;
		}
		//////////////////////////////
		//Build text fields.
		//////////////////////////////
		public function drawTextfield (font:*, sharp:Number, thick:Number, col:uint, xpos:int, ypos:int, wd:int, txtht:int, txtfontsize:Number, txtback:Boolean, txtbackcol:uint, txtfldborder:Boolean, txtfldbordercol:uint, txtfldtype:String, txtalign:String, txtselect:Boolean, txtname:String, txtembed:Boolean, wordwrap:Boolean = true, multiline:Boolean = true, underline:Boolean = false):TextField {
			_textfield = new TextField();
			if (font == "_sans") {
				_txtformat.font = "_sans";
				_textfield.embedFonts = false;
			} else {
				_txtfont = new font;
				_txtformat.font = _txtfont.fontName;
				_textfield.embedFonts = txtembed;
			}
			_txtformat.size = txtfontsize;
			_txtformat.underline = underline;
			switch (txtalign) {
				case 'center' :
					_txtformat.align = TextFormatAlign.CENTER;
				break;
				case 'justify' :
					_txtformat.align = TextFormatAlign.JUSTIFY;
				break;
				case 'left' :
					_txtformat.align = TextFormatAlign.LEFT;
				break;
				case 'right' :
					_txtformat.align = TextFormatAlign.RIGHT;
				break;
			}
			switch (txtfldtype) {
				case 'input' :
					_textfield.type = TextFieldType.INPUT;
				break;
				case 'dynamic' :
					_textfield.type = TextFieldType.DYNAMIC;
				break;
			}
			_textfield.antiAliasType = AntiAliasType.ADVANCED;
			_textfield.sharpness = sharp;
			_textfield.thickness = thick;
			_textfield.alwaysShowSelection = true;
			_textfield.mouseWheelEnabled = true;
			_textfield.condenseWhite = true;
			_textfield.wordWrap = wordwrap;
			_textfield.multiline = multiline;
			_textfield.border = txtfldborder;
			_textfield.borderColor = txtfldbordercol;
			_textfield.selectable = txtselect;
			_textfield.background = txtback;
			_textfield.backgroundColor = txtbackcol;
			_textfield.textColor = col;
			if(txtht as String == 'auto') {
				_textfield.autoSize = TextFieldAutoSize.LEFT;
			} else {
				_textfield.height = txtht as Number;
			}
			_textfield.width = wd;
			_textfield.x = xpos;
			_textfield.y = ypos;
			_textfield.name = txtname;
			_textfield.defaultTextFormat = _txtformat;
			_textfield.setTextFormat (_txtformat);
			_textfield.cacheAsBitmap;
			return _textfield;
		}
		//////////////////////////////
		//Build colorpicker.
		//////////////////////////////
		public function buildColorPicker (xpos:int, ypos:int):ColorPicker {
			_picker = new ColorPicker();
			_picker.x = xpos;
			_picker.y = ypos;
			_picker.editable = true;
			_picker.setStyle("columnCount", 24);
			_picker.setStyle("swatchWidth", 8);
			_picker.setStyle("swatchHeight", 12);
			_picker.setStyle("swatchPadding", 1);
			_picker.setStyle("backgroundPadding", 2);
			_picker.setStyle("textPadding", 4);
			_picker.cacheAsBitmap;
			return _picker;
		}
		//////////////////////////////
		//Build slider.
		//////////////////////////////
		public function buildSlider (xpos:int, ypos:int, dir:String, nummax:Number):Slider {
			_slider = new Slider();
			switch(dir) {
				case 'vertical':
					_slider.direction = SliderDirection.VERTICAL;
				break;
				case 'horizontal':
					_slider.direction = SliderDirection.HORIZONTAL;
				break;
			}
			_slider.minimum = 0;
			_slider.maximum = nummax;
			_slider.value = 1;
			_slider.liveDragging = true;
			_slider.snapInterval = 1;
			_slider.tickInterval = _slider.snapInterval;
			_slider.move(xpos, ypos);
			_slider.validateNow();
			_slider.cacheAsBitmap;
			return _slider;
		}

		//////////////////////////////
		//Build list.
		//////////////////////////////
		public function buildList(dp:DataProvider):List {
			_txtformat.font = "_sans";
			_txtformat.size = 12;
			_txtformat.color = 0xffffff;

			_list = new List();
			_list.iconField = "iconSource";
			_list.dataProvider = dp;
			_list.cacheAsBitmap;
			_list.setStyle("contentPadding", 5);
			_list.verticalScrollPolicy = 'auto';
			StyleManager.setStyle('textFormat', _txtformat);
			StyleManager.setStyle('embedFonts', false);
			return _list;
		}
		//////////////////////////////
		//Apply tint to an object.
		//////////////////////////////
		public function applyTint(tintobject:Sprite, tintcolor:uint, tintscale:Number):void {
			var tint:Color = new Color();
			tint.setTint(tintcolor, tintscale);
			tintobject.transform.colorTransform = tint;
		}
		//////////////////////////////

		//////////////////////////////
		//Private, internal methods.
		//////////////////////////////

		//////////////////////////////

		//////////////////////////////
		//Check available component styles.
		//////////////////////////////
		private function checkStyles(comp:*):void {
			var styleObj:Object = comp.getStyleDefinition();
			for (var st:String in styleObj) {
				trace("Style: " + st + " Value: " + styleObj[st]);
			}
		}
		//////////////////////////////
	}
}

